/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author sorasaks
 */
public class ProcessUbuntu {

    public static final String TEMP_SCRIPT_FILENAME = "temp_script.sh";
    private String inputString;
    private StringBuilder command;
    private String outputString;
    private String errorString;
    private String filename;
    private String filePath;

    public ProcessUbuntu(String filePath) {
        this.filePath = filePath;
    }

    public ProcessUbuntu(String filePath, StringBuilder command) {
        this.filePath = filePath;
        this.filename = filePath + "/" + TEMP_SCRIPT_FILENAME;
        this.command = command;
    }

    public String getErrorString() {
        return this.errorString;
    }

    public String getOutputString() {
        return this.outputString;
    }

    public void process() {
        if (command != null) {
            generateScript(command);
        }
        runProcessScript();
    }

    public void process(StringBuilder command) {
        this.command = command;
        generateScript(command);
        runProcessScript();
    }

    public void generateScript(StringBuilder command) {
        FileUtil.createFile(filename, command);
    }

    public void runProcessScript() {

        Process p = null;
        int returnCode = -1;
        ProcessBuilder pb = new ProcessBuilder();
        pb.command("bash", "-c", "./" + TEMP_SCRIPT_FILENAME);
        if (filePath != null) {
            pb.directory(new File(filePath));
        }

        BufferedReader stdInput = null;
        BufferedReader stdError = null;

        try {
            p = pb.start();

            stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            outputString = "";
            String line;
            while ((line = stdInput.readLine()) != null) {
                System.out.println(line);
                outputString += line + "\n";
            }

            errorString = "";
            while ((line = stdError.readLine()) != null) {
                System.out.println(line);
                errorString += line + "\n";
            }

            System.out.println("Command executed");
        } catch (Exception ex) {
            System.out.println(ex);
        }

        try {
            returnCode = p.waitFor();
        } catch (InterruptedException ex) {
            System.out.println(ex);
        }

        System.out.println(returnCode);
    }

    public void process(String command) {
        this.inputString = command;
        runProcess();
    }

    private void runProcess() {
        Process p = null;
        int returnCode = -1;

        BufferedReader stdInput = null;
        BufferedReader stdError = null;

        try {
            p = Runtime.getRuntime().exec(inputString);

            stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            outputString = "";
            String line;
            while ((line = stdInput.readLine()) != null) {
                System.out.println(line);
                outputString += line + "\r\n";
            }

            errorString = "";
            while ((line = stdError.readLine()) != null) {
                System.out.println(line);
                errorString += line + "\r\n";
            }

            System.out.println("Command executed");
        } catch (IOException ex) {
            System.out.println(ex);
        }

        try {
            returnCode = p.waitFor();
        } catch (InterruptedException ex) {
            System.out.println(ex);
        }

        System.out.println(returnCode);
    }
}
